// mircoApp/ai-tongue-diagnosis/ai_tongue_diagnosis_information/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    diaImage: "",
    isClick: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      diaImage: options.img
    })
    wx.setNavigationBarColor({
      backgroundColor: '#FFAA01',
      frontColor: '#ffffff',
    })
    const theme_color = wx.getStorageSync('yz_basic_info').theme_color;
    this.setData({
      theme_color: theme_color
    })
  },
  toMember() {
    wx.navigateTo({
      url: '/mircoApp/ai-tongue-diagnosis/ai_tongue_diagnosis_member/index',
    })
  },
  detectionTap() {
    if (!this.data.isClick) return
    this.setData({
      isClick: false
    })
    setTimeout(() => {
      if (!this.data.info.age) {
        wx.showModal({
          content: '请填写性别或年龄',
          confirmColor: '#FFAA01',
          cancelColor: '#7E7E7E',
          complete: (res) => {
            if (res.confirm) {
              wx.navigateTo({
                url: '/mircoApp/ai-tongue-diagnosis/ai_tongue_diagnosis_member/index',
              })
            }
            this.setData({
              isClick: true
            })
          }
        })
        return
      }
      if (!this.data.diaImage) {
        app.tips("未检测到舌头，请按照要求重新拍照检测");
        this.setData({
          isClick: true
        })
        return
      }
      // wx.showLoading({
      //   title: '检测图片中',
      // })
      let urlStr = app.getNetAddresss('upload.uploadPic');
      urlStr += '&local_upload=1'
      wx.uploadFile({
        url: urlStr,
        filePath: this.data.diaImage,
        name: 'file',
        formData: null,
        success: (res) => {
          let dataInfo = JSON.parse(res.data).data;
          let img_loc = dataInfo.img;
          this.uploadDiagnosis(img_loc);
        },
        fail: (err) => {
          wx.hideLoading()
          app.tips(err);
          this.setData({
            isClick: true
          })
        }
      });
    }, 2500);

  },
  async uploadDiagnosis(img_loc) {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.ai-tongue-diagnosis.frontend.controllers.detection.detection"),
      data: {
        file: img_loc
      }
    })
    if (data.result) {
      wx.redirectTo({
        url: `/mircoApp/ai-tongue-diagnosis/ai_tongue_diagnosis_report/ai_tongue_diagnosis_report?id=${data.data.id}`,
      })
      this.setData({
        isClick: true
      })
    } else {
      app.tips(data.msg);
      this.setData({
        diaImage: ""
      })
      this.setData({
        isClick: true
      })
    }
    setTimeout(() => {
      wx.hideLoading();
    }, 1000);
  },
  rephotograph() {
    if (!this.data.isClick) return
    this.setData({
      isClick: false
    })
    wx.redirectTo({
      url: '/mircoApp/ai-tongue-diagnosis/ai_tongue_diagnosis/index',
      success: (e) => {
        this.setData({
          isClick: true
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.ai-tongue-diagnosis.frontend.controllers.index.member-info")
    })
    if (data.result) {
      console.log(data)
      this.setData({
        info: data.data.info
      })
    } else {
      app.tips(data.msg);
    }
  }
})